<template>
  <div style="padding: 10px;">
    <Row :space="15">
      <Cell>
        <Input v-model="formData.parameterName" placeholder="参数名称" style="width: 100%;" />
      </Cell>
      <Cell>
        <Input v-model="formData.name" placeholder="节点名称" style="width: 100%;" />
      </Cell>
      <Cell>
        <Select v-model="formData.bugType" :datas="bugTypeData" keyName="value" titleName="label"
          placeholder="爬取类型"></Select>
      </Cell>
      <Cell>
        <Input v-model="formData.website" placeholder="爬取网点" style="width: 100%;" />
      </Cell>
      <Cell>
        <AutoComplete v-model="formData.text" :mustMatch="false" type="title" :datas="parameters" placeholder="搜索文本" />
      </Cell>
      <Cell>
        <div class="h-input-group" v-for="(item, i) in formData.selecters" :key="i" style="margin-bottom: 10px;">
          <Input v-model="item.selecterKey" placeholder="保存名称" />
          <span class="h-input-addon">-</span>
          <Input v-model="item.selecter" placeholder="标签选择器" />
          <span class="h-input-addon link" @click="delSelecter(i)">删除</span>
        </div>
      </Cell>
      <Cell>
        <span class="link" @click="addSelecter">添加一行</span>
      </Cell>
      <Cell>
        <Checkbox v-model="formData.printInput" placeholder="跟踪打印" title="跟踪打印">打印输入</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.printOutput" title="跟踪打印">打印输出</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.inputHistory" title="会话历史">保存输入</Checkbox>
        <Space :width="15" />
        <Checkbox v-model="formData.resultHistory" title="会话历史">保存输出</Checkbox>
      </Cell>
      <Cell>
        <Space :width="15" />
        <Button color="primary" @click="bindForm">设置</Button>
        <Space :width="15" />
        <Button @click="setForm" color="yellow">重置</Button>
      </Cell>
    </Row>
  </div>
</template>

<script>
import LogicFlow from '@logicflow/core';
import { message } from 'heyui';

export default {
  props: {
    lf: Object,
    currentNode: Object,
  },
  mounted() {
    this.setForm();
    this.getWebsites();
    this.getParameters();
  },
  data() {
    return {
      websites: [],
      bugTypeData: [{ label: "爬取链接", value: "link" }, { label: "爬取文本", value: "text" }],
      parameters: [],
      formData: {
        bugType: "link",
        website: "",
        selecters: [],
        text: "",
      }
    }
  },
  methods: {
    setForm() {
      this.formData.name = this.$props.currentNode.data.text.value;
      let id = this.$props.currentNode.data.id;
      let properties = this.$props.lf.getProperties(id);
      this.formData.parameterName = properties.parameterName;
      this.formData.website = properties.website;
      this.formData.text = properties.text;
      if (!!properties.bugType) {
        this.formData.bugType = properties.bugType;
      }
      if (!!properties.selecters) {
        this.formData.selecters = properties.selecters;
      }
      this.formData.printInput = properties.printInput;
      this.formData.printOutput = properties.printOutput;
      this.formData.inputHistory = properties.inputHistory;
      if (!!properties.resultHistory) {
        this.formData.resultHistory = properties.resultHistory;
      }
    },
    bindForm() {
      let id = this.$props.currentNode.data.id;
      this.$props.lf.updateText(id, this.formData.name);
      this.$props.lf.setProperties(id, Object.assign({ ...this.formData }, {
        similarityScore: Number((this.formData.similarityScore * 0.1).toFixed(2)),
      }));
      this.$emit('close', false);
      message.success("设置成功");
    },

    addSelecter() {
      this.formData.selecters.push({ selecterKey: "", selecter: "", })
    },
    delSelecter(index) {
      this.formData.selecters.splice(index, 1);
    },
    getWebsites() {
      this.$post("/crawlerselecter/list", { page: { current: 1, size: 1000 } }).then(res => {
        if (res.data.code == this.$success) {
          this.websites = res.data.data.rows;
        } else {
          this.websites = [];
          message.error(res.data.msg);
        }
      })
    },

    getParameters() {
      let flowData = (this.$props.lf).getGraphRawData();
      let nodes = flowData["nodes"];
      let node = this.getStartNode(flowData);
      while (!!node && node?.id != this.$props.currentNode?.data?.id && node?.type != 'EndNode') {
        var properties = node?.properties;
        var name = properties?.name;
        var parameterName = properties?.parameterName;

        if (!!parameterName) {
          if (properties?.inputHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".input", value: "$" + parameterName + ".input" });
          } if (properties?.resultHistory == true) {
            this.parameters.push({ label: "$" + parameterName + ".output", value: "$" + parameterName + ".output" });
          }
        }
        node = this.getNextNode(flowData, node.id);
      }
    },

    getStartNode(flowData) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.type == 'StartNode') {
          return node;
        }
      }
    },
    getNode(flowData, id) {
      let nodes = flowData["nodes"];
      for (var i = 0; i < nodes.length; i++) {
        let node = nodes[i];
        if (node.id == id) {
          return node;
        }
      }
    },
    getNextNode(flowData, id) {
      let edges = flowData["edges"]
      for (var i = 0; i < edges.length; i++) {
        let edge = edges[i];
        if (edge.sourceNodeId == id) {
          return this.getNode(flowData, edge.targetNodeId);
        }
      }
    },
  }
}
</script>